package com.sky.controller.admin;

import com.sky.dto.CreateAddressDTO;
import com.sky.dto.UpdateAddressDTO;
import com.sky.service.AddressService;
import com.sky.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/students/addresses")
public class AddressController {
    @Autowired
    private AddressService addressService;

    // 获取学生地址列表
    @GetMapping
    public ResponseEntity<List<AddressVO>> getAddressList(@RequestParam Long studentId) {
        List<AddressVO> addressList = addressService.getAddressList(studentId);
        return ResponseEntity.ok(addressList);
    }

    // 获取单个地址信息
    @GetMapping("/{id}")
    public ResponseEntity<AddressVO> getAddressById(@PathVariable Long id) {
        AddressVO addressVO = addressService.getAddressById(id);
        if (addressVO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(addressVO);
    }

    // 创建学生地址
    @PostMapping
    public ResponseEntity<Long> createAddress(@RequestBody CreateAddressDTO createAddressDTO) {
        Long id = addressService.createAddress(createAddressDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(id);
    }

    // 更新学生地址
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateAddress(@PathVariable Long id, @RequestBody UpdateAddressDTO updateAddressDTO) {
        updateAddressDTO.setId(id);
        addressService.updateAddress(updateAddressDTO);
        return ResponseEntity.noContent().build();
    }

    // 删除学生地址
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAddress(@PathVariable Long id) {
        addressService.deleteAddress(id);
        return ResponseEntity.noContent().build();
    }

    // 设置默认地址
    @PutMapping("/{id}/default")
    public ResponseEntity<Void> setDefaultAddress(@PathVariable Long id) {
        addressService.setDefaultAddress(id);
        return ResponseEntity.noContent().build();
    }
}